$(document).ready(function(){
$('#start_date').datetime({ withTime: false, format: 'yy-mm-dd' });
$('#end_date').datetime({ withTime: false, format: 'yy-mm-dd' });

$('input.check_all').click(function() {
	if ($(this).attr('checked') == 'checked') {
		$('li.unit input').attr('checked', 'checked');
	} else {
		$('li.unit input').removeAttr('checked');
	}
});
var url = '/traffic/bandwidth_domain/?service_id=0&'+Math.random();
$('#service_id').change(function() {
	var service_id = $(this).val();
	url = '/traffic/bandwidth_domain/?service_id='+service_id+'&'+Math.random();
	$.get(url, function(data) {
		$('td.unit').html(data);
		$('input.check_all').click(function() {
			if ($(this).attr('checked') == 'checked') {
				$(this).parent().parent().find('input.domain').attr('checked', 'checked');
			} else {
				$(this).parent().parent().find('input.domain').removeAttr('checked');
			}
		});
	}, 'html');	
});


$('#search').submit(function() {
	var msg = '错误信息:'+"\n";

	var str = [];
	$('input.domain :checked').each(function() {
		str.push($(this).val()); 
	});
	var unit = str.join('_');
	var unit_flag = true;
/*
	if (unit == '') {
		msg += '请选择域名'+"\n";
		unit_flag = false;	
	}
*/

	var service_id = $('#service_id').val();
	var service_flag = true;
	if (service_id == 0) {
		msg += '请选择业务类型'+"\n";
		service_flag = false;	
	}	
	var start = $('#start_date').val();
	var start_flag = true;
	if (start == '') {
		msg += '请选择起始时间'+"\n";
		start_flag = false;
	}
	var end = $('#end_date').val();
	var end_flag = true;
	if (end == '') {
		msg += '请选择起始时间'+"\n";
		end_flag = false;
	}
	if (unit_flag && service_flag && start_flag && end_flag) {
		var queryString = $('#search').formSerialize();
		$.post('/traffic/domain_chart/', queryString, function(data) {
			$('#placeholder').empty();
			$('tr.data').remove();
			//var d = new Array();
			//$.each(data, function(index, value) {
				//d.push({'label':value.info.unit, 'data':value.data});
				//$('#tableholder table').append('<tr class="data"><td>'+value.info.unit+'</td><td>'+value.peak+'</td><td>'+value.total+'</td><td>'+value.peak_time+'</td></tr>');
			//});
			//$('#tableholder').show();
			$.plot($("#placeholder"), [data.data], { grid: { hoverable: true, clickable: true },xaxis: { mode: "time",timeformat: "/%m/%d" } });
		}, 'json');
	} else {
		alert(msg);
	}
	return false;
});

Date.prototype.pattern=function(fmt) {     
    var o = {     
    "M+" : this.getMonth()+1, //月份     
    "d+" : this.getDate(), //日     
    "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时     
    "H+" : this.getHours(), //小时     
    "m+" : this.getMinutes(), //分     
    "s+" : this.getSeconds(), //秒     
    "q+" : Math.floor((this.getMonth()+3)/3), //季度     
    "S" : this.getMilliseconds() //毫秒     
    };     
    var week = {     
    "0" : "\u65e5",     
    "1" : "\u4e00",     
    "2" : "\u4e8c",     
    "3" : "\u4e09",     
    "4" : "\u56db",     
    "5" : "\u4e94",     
    "6" : "\u516d"    
    };     
    if(/(y+)/.test(fmt)){     
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));     
    }     
    if(/(E+)/.test(fmt)){     
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]);     
    }     
    for(var k in o){     
        if(new RegExp("("+ k +")").test(fmt)){     
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));     
        }     
    }     
    return fmt;     
} 

var today = new Date();
var yesterday = new Date();
yesterday.setTime(yesterday.getTime()-24*60*60*1000);
var monday = new Date();
monday.setTime(monday.getTime() - 24*60*60*1000*monday.getDay());
var month = new Date();
month.setTime(month.getTime() - 24*60*60*1000*(month.getDate()-1));

var last_month_end = new Date();
last_month_end.setDate(1);
last_month_end.setTime(last_month_end.getTime() - 24*60*60*1000);

var last_month_start = new Date();
last_month_start.setMonth(last_month_start.getMonth() -1);
last_month_start.setDate(1);

$('a.today').click(function() {
	$('#start_date').val(today.pattern("yyyy-MM-dd"));
	$('#end_date').val(today.pattern("yyyy-MM-dd"));
});
$('a.yesterday').click(function() {
	$('#start_date').val(yesterday.pattern("yyyy-MM-dd"));
	$('#end_date').val(yesterday.pattern("yyyy-MM-dd"));
});
$('a.week').click(function() {
	$('#start_date').val(monday.pattern("yyyy-MM-dd"));
	$('#end_date').val(today.pattern("yyyy-MM-dd"));
});
$('a.month').click(function() {
	$('#start_date').val(month.pattern("yyyy-MM-dd"));
	$('#end_date').val(today.pattern("yyyy-MM-dd"));
});
$('a.last_month').click(function() {
	$('#start_date').val(last_month_start.pattern("yyyy-MM-dd"));
	$('#end_date').val(last_month_end.pattern("yyyy-MM-dd"));
});

});
